/**
 * 作者: hxl
 * 日期: 2025-05-16 17:14:56
 * 备注: 用户联盟乱斗信息表
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBUserUnionScuffle;
import com.motu.vertx.module.utility.model.BaseModel;
import com.motu.vertx.module.utility.model.Model;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;

public class UserUnionScuffleModel extends BaseModel implements Model {
    public static final String LISTKEY = "act_list_key.enemy_union_id";
    public static final int REDIS_EXPIRE_TIME = 0;// redis过期时间
    public static final int UPDATE_DB_TYPE = UPDATE_DB_TYPE_QUEUE;// 数据入库类型 队列入库
    public static final boolean ORDER_LIST = false;// 是否需要额外的有序列表

    protected long id; // ID
    protected long userId; // 玩家id
    protected long actId; // 活动id
    protected long enemyUnionId; // 对手联盟id
    protected long matchCdTime; // 匹配冷却时间
    protected String defeatedMembers = ""; // 已打败的成员
    protected long initHp; // 初始hp
    protected long curHp; // 当前hp
    protected long initPower; // 初始实力
    protected int energy; // 剩余精力
    protected String chooseList = ""; // 待选择成员列表
    protected int addPerType; // 战斗加成类型
    protected int winingStreak; // 连胜次数
    protected int subScore; // 对方联盟扣的分数
    protected int matchType; // 匹配类型 (0：随机匹配；1：下战书)
    protected String failedMembers = ""; // 挑战失败的成员
    protected String actListKey = ""; // 活动分组key (user_id.act_id)

    public void reset() {
    }

    protected String getKey() {
        return "UserUnionScuffle." + actListKey + "." + enemyUnionId;
    }

    protected String getListKey() {
        return "UserUnionScuffle." + actListKey;
    }

    protected String getListItemKey() {
        return enemyUnionId + "";
    }

    public void doCreate() {
        setTableId();
        insert();
    }

    public void setTableId() {
        putId(incrTableId("user_union_scuffle"));
    }

    public JsonObject insert() {
        return super.insert(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    public void delete() {
        super.delete(getKey(), id, UPDATE_DB_TYPE, userId, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    /**
     * 根据入库类型更新
     */
    public JsonObject update() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, userId, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    /**
     * 实时入队列
     */
    public JsonObject updateQueueNow() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    public JsonObject getAllObj(){
        JsonObject obj = new JsonObject();
        obj.put("id", id); // ID
        obj.put("userId", userId); // 玩家id
        obj.put("actId", actId); // 活动id
        obj.put("enemyUnionId", enemyUnionId); // 对手联盟id
        obj.put("matchCdTime", matchCdTime); // 匹配冷却时间
        obj.put("defeatedMembers", defeatedMembers); // 已打败的成员
        obj.put("initHp", initHp); // 初始hp
        obj.put("curHp", curHp); // 当前hp
        obj.put("initPower", initPower); // 初始实力
        obj.put("energy", energy); // 剩余精力
        obj.put("chooseList", chooseList); // 待选择成员列表
        obj.put("addPerType", addPerType); // 战斗加成类型
        obj.put("winingStreak", winingStreak); // 连胜次数
        obj.put("subScore", subScore); // 对方联盟扣的分数
        obj.put("matchType", matchType); // 匹配类型 (0：随机匹配；1：下战书)
        obj.put("failedMembers", failedMembers); // 挑战失败的成员
        obj.put("actListKey", actListKey); // 活动分组key (user_id.act_id)
        return obj;
    }

    public PBUserUnionScuffle.Builder takeInitBuilder() {
        PBUserUnionScuffle.Builder sb = PBUserUnionScuffle.newBuilder();
        if (enemyUnionId != 0) {
            sb.setEnemyUnionId(enemyUnionId);
        }
        if (matchCdTime != 0) {
            sb.setMatchCdTime(matchCdTime);
        }
        if (this.defeatedMembers != null && this.defeatedMembers.length() > 0) {
            JsonArray list = new JsonArray(this.defeatedMembers);
            for(int i=0; i<list.size(); i++) {
                sb.addDefeatedMembers(list.getLong(i));
            }
        }
        if (initHp != 0) {
            sb.setInitHp(initHp);
        }
        if (curHp != 0) {
            sb.setCurHp(curHp);
        }
        if (initPower != 0) {
            sb.setInitPower(initPower);
        }
        if (energy != 0) {
            sb.setEnergy(energy);
        }
        if (addPerType != 0) {
            sb.setAddPerType(addPerType);
        }
        return sb;
    }

    public PBUserUnionScuffle.Builder takeBuilder() {
        if (updateObj == null || updateObj.isEmpty()) {
            return takeInitBuilder();
        }
        PBUserUnionScuffle.Builder sb = PBUserUnionScuffle.newBuilder();
        if (isFieldUpdate("enemyUnionId")) {
            sb.setEnemyUnionId(enemyUnionId);
        }
        if (isFieldUpdate("matchCdTime")) {
            sb.setMatchCdTime(matchCdTime);
        }
        if (isFieldUpdate("defeatedMembers")) {
            if (this.defeatedMembers != null && this.defeatedMembers.length() > 0) {
                JsonArray list = new JsonArray(this.defeatedMembers);
                for(int i=0; i<list.size(); i++) {
                    sb.addDefeatedMembers(list.getLong(i));
                }
                if (list.isEmpty()) {
                    sb.clearDefeatedMembers();
                }
            } else {
                sb.clearDefeatedMembers();
            }
        }
        if (isFieldUpdate("initHp")) {
            sb.setInitHp(initHp);
        }
        if (isFieldUpdate("curHp")) {
            sb.setCurHp(curHp);
        }
        if (isFieldUpdate("initPower")) {
            sb.setInitPower(initPower);
        }
        if (isFieldUpdate("energy")) {
            sb.setEnergy(energy);
        }
        if (isFieldUpdate("addPerType")) {
            sb.setAddPerType(addPerType);
        }
        return sb;
    }

    public long getId() {
        return id;
    }

    public void putId(long id) {
        if (this.id != id) {
            this.id = id;
            updateRedisHashField(getKey(), "id", this.id);
        }
    }

    public void addId(long value) {
        this.id += value;
        updateRedisHashField(getKey(), "id", this.id);
    }

    public long getUserId() {
        return userId;
    }

    public void putUserId(long userId) {
        if (this.userId != userId) {
            this.userId = userId;
            updateRedisHashField(getKey(), "userId", this.userId);
        }
    }

    public void addUserId(long value) {
        this.userId += value;
        updateRedisHashField(getKey(), "userId", this.userId);
    }

    public long getActId() {
        return actId;
    }

    public void putActId(long actId) {
        if (this.actId != actId) {
            this.actId = actId;
            updateRedisHashField(getKey(), "actId", this.actId);
        }
    }

    public void addActId(long value) {
        this.actId += value;
        updateRedisHashField(getKey(), "actId", this.actId);
    }

    public long getEnemyUnionId() {
        return enemyUnionId;
    }

    public void putEnemyUnionId(long enemyUnionId) {
        if (this.enemyUnionId != enemyUnionId) {
            this.enemyUnionId = enemyUnionId;
            updateRedisHashField(getKey(), "enemyUnionId", this.enemyUnionId);
        }
    }

    public void addEnemyUnionId(long value) {
        this.enemyUnionId += value;
        updateRedisHashField(getKey(), "enemyUnionId", this.enemyUnionId);
    }

    public long getMatchCdTime() {
        return matchCdTime;
    }

    public void putMatchCdTime(long matchCdTime) {
        if (this.matchCdTime != matchCdTime) {
            this.matchCdTime = matchCdTime;
            updateRedisHashField(getKey(), "matchCdTime", this.matchCdTime);
        }
    }

    public void addMatchCdTime(long value) {
        this.matchCdTime += value;
        updateRedisHashField(getKey(), "matchCdTime", this.matchCdTime);
    }

    public JsonArray getDefeatedMembersArray() {
        if (this.defeatedMembers != null && this.defeatedMembers.length() > 0) {
            return new JsonArray(this.defeatedMembers);
        } else {
            return new JsonArray();
        }
    }

    public String getDefeatedMembers() {
        return defeatedMembers;
    }

    public void putDefeatedMembers(String defeatedMembers) {
        if (!this.defeatedMembers.equals(defeatedMembers)) {
            this.defeatedMembers = defeatedMembers;
            updateRedisHashField(getKey(), "defeatedMembers", this.defeatedMembers);
        }
    }

    public long getInitHp() {
        return initHp;
    }

    public void putInitHp(long initHp) {
        if (this.initHp != initHp) {
            this.initHp = initHp;
            updateRedisHashField(getKey(), "initHp", this.initHp);
        }
    }

    public void addInitHp(long value) {
        this.initHp += value;
        updateRedisHashField(getKey(), "initHp", this.initHp);
    }

    public long getCurHp() {
        return curHp;
    }

    public void putCurHp(long curHp) {
        if (this.curHp != curHp) {
            this.curHp = curHp;
            updateRedisHashField(getKey(), "curHp", this.curHp);
        }
    }

    public void addCurHp(long value) {
        this.curHp += value;
        updateRedisHashField(getKey(), "curHp", this.curHp);
    }

    public long getInitPower() {
        return initPower;
    }

    public void putInitPower(long initPower) {
        if (this.initPower != initPower) {
            this.initPower = initPower;
            updateRedisHashField(getKey(), "initPower", this.initPower);
        }
    }

    public void addInitPower(long value) {
        this.initPower += value;
        updateRedisHashField(getKey(), "initPower", this.initPower);
    }

    public int getEnergy() {
        return energy;
    }

    public void putEnergy(int energy) {
        if (this.energy != energy) {
            this.energy = energy;
            updateRedisHashField(getKey(), "energy", this.energy);
        }
    }

    public void addEnergy(int value) {
        this.energy += value;
        updateRedisHashField(getKey(), "energy", this.energy);
    }

    public JsonArray getChooseListArray() {
        if (this.chooseList != null && this.chooseList.length() > 0) {
            return new JsonArray(this.chooseList);
        } else {
            return new JsonArray();
        }
    }

    public String getChooseList() {
        return chooseList;
    }

    public void putChooseList(String chooseList) {
        if (!this.chooseList.equals(chooseList)) {
            this.chooseList = chooseList;
            updateRedisHashField(getKey(), "chooseList", this.chooseList);
        }
    }

    public int getAddPerType() {
        return addPerType;
    }

    public void putAddPerType(int addPerType) {
        if (this.addPerType != addPerType) {
            this.addPerType = addPerType;
            updateRedisHashField(getKey(), "addPerType", this.addPerType);
        }
    }

    public void addAddPerType(int value) {
        this.addPerType += value;
        updateRedisHashField(getKey(), "addPerType", this.addPerType);
    }

    public int getWiningStreak() {
        return winingStreak;
    }

    public void putWiningStreak(int winingStreak) {
        if (this.winingStreak != winingStreak) {
            this.winingStreak = winingStreak;
            updateRedisHashField(getKey(), "winingStreak", this.winingStreak);
        }
    }

    public void addWiningStreak(int value) {
        this.winingStreak += value;
        updateRedisHashField(getKey(), "winingStreak", this.winingStreak);
    }

    public int getSubScore() {
        return subScore;
    }

    public void putSubScore(int subScore) {
        if (this.subScore != subScore) {
            this.subScore = subScore;
            updateRedisHashField(getKey(), "subScore", this.subScore);
        }
    }

    public void addSubScore(int value) {
        this.subScore += value;
        updateRedisHashField(getKey(), "subScore", this.subScore);
    }

    public int getMatchType() {
        return matchType;
    }

    public void putMatchType(int matchType) {
        if (this.matchType != matchType) {
            this.matchType = matchType;
            updateRedisHashField(getKey(), "matchType", this.matchType);
        }
    }

    public void addMatchType(int value) {
        this.matchType += value;
        updateRedisHashField(getKey(), "matchType", this.matchType);
    }

    public JsonArray getFailedMembersArray() {
        if (this.failedMembers != null && this.failedMembers.length() > 0) {
            return new JsonArray(this.failedMembers);
        } else {
            return new JsonArray();
        }
    }

    public String getFailedMembers() {
        return failedMembers;
    }

    public void putFailedMembers(String failedMembers) {
        if (!this.failedMembers.equals(failedMembers)) {
            this.failedMembers = failedMembers;
            updateRedisHashField(getKey(), "failedMembers", this.failedMembers);
        }
    }

    public String getActListKey() {
        return actListKey;
    }

    public void putActListKey(String actListKey) {
        if (!this.actListKey.equals(actListKey)) {
            this.actListKey = actListKey;
            updateRedisHashField(getKey(), "actListKey", this.actListKey);
        }
    }

}